package presentation.models;


import java.util.Arrays;
import java.util.Set;

import javax.swing.DefaultListModel;

import domain.searchEngine.SearchResult;

/**
 * list model for the presented search results
 * manages all the presented posts and their scores
 */
public class SearchResListModel extends DefaultListModel
{
	
	private static final long serialVersionUID = -721812096736561064L ;

	/**
	 * constructor
	 */
	public SearchResListModel(){
		super();
	}
	
	/**
	 * sets the list model to hold the given search results ordered by their scores
	 * @param matches - the matches to be presented as the search result
	 */
	public void setSearchResults(Set<SearchResult> matches){
		this.removeAllElements();
		//add the matches (if there are any) sorted by their scores:
		if((matches!=null) && (matches.size()>0)){
			SearchResult[] sortedMatches = new SearchResult[matches.size()];
			sortedMatches = matches.toArray(sortedMatches);
			Arrays.sort(sortedMatches);
			for(int i=0; i<sortedMatches.length; i++){
				this.addElement(sortedMatches[i]);
			}
		}
	}
}
